//index.js
//获取应用实例
//index.js
//获取应用实例
var app, dsq, dsq1, a = getApp()
var WXBizDataCrypt = require('../../utils/RdWXBizDataCrypt.js');


Page({
    data: {
      is__authorize:false,
      userInfo: {},
      dataInfo: {},
      userList:{},
      price_id:[],
      step:0,
      isTiao:0,
      canIUse: wx.canIUse('button.open-type.getUserInfo'),
	  opid : '',

  }, bindGetUserInfo: function (t) {
    console.log(t), "getUserInfo:ok" == t.detail.errMsg && (this.setData({
      hydl: !1
    }), this.changeData());
  },
  changeData: function () {
    var n = this;
    wx.getSetting({
      success: function (t) {
        console.log(t), t.authSetting["scope.userInfo"] ? wx.getUserInfo({
          success: function (t) {
            console.log(t), a.util.request({
              url: "entry/wxapp/login",
              cachetime: "0",
              data: {
                openid: getApp().getOpenId,
                headimgurl: t.userInfo.avatarUrl,
                nickname: t.userInfo.nickName
              },
              header: {
                "content-type": "application/json"
              },
              dataType: "json",
              success: function (t) {
                console.log("用户信息", t);
              }
            });
            var e = t.userInfo;
            console.log(e), n.setData({
              userInfo: t.userInfo,
              is__authorize: true
            });
          }
        }) : (console.log("未授权过"), n.setData({
            is__authorize: false,
          hydl: !0
        }));
      }
    });
  },
    onLoad: function(options) {
      var o = this;
      a.getUserInfo(function (e) {
        var u = a.globalData.userInfo;
        if (null != options.op) {
			console.log('---' , u), a.util.request({
				url: "entry/wxapp/banding",
				cachetime: "0",
				data: {
          op: options.op
				},
				success: function (t) {
					console.log(t);
				}
			});
		}
        if (u.nickname) {
          o.setData({
            userInfo: a.globalData.userInfo,
            is__authorize:true,
            is_show:false
          });
        }else{
          o.setData({
            is__authorize: false
          });
        }
        o.getDate();//请求数据
        o.getData(getApp().getSK,'wxf288980faf9318ca');
        
      }), a.util.request({
        url: "entry/wxapp/system",
        cachetime: "0",
        success: function (e) {
          var t = e.data;
          o.setData({
            xtxx: t
          }), getApp().xtxx1 = t;
        }
        });
      o.getUserList();
    },
    getDate: function(){
      var o= this;
      a.util.request({
        url: "entry/wxapp/index",
        cachetime: "0",
        data: {
          openid: getApp().getOpenId,
        },
        header: {
          "content-type": "application/json"
        },
        dataType: "json",
        success: function (t) {
          console.log(t);
          o.setData({
            dataInfo: t.data
          })
        }
      });
    },
    bandLuck: function(){
      this.getUserList('Luck');
    },
    bandMorning: function () {
      this.getUserList('Morning');
    },
    bandWill: function () {
      this.getUserList('Will');
    },
    getUserList: function (type='Luck'){
      var o = this;
      o.setData({
        'type': type
      });
      a.util.request({
        url: "entry/wxapp/jrmx",
        cachetime: "0",
        data: {
          type:type,
        },
        header: {
          "content-type": "application/json"
        },
        dataType: "json",
        success: function (t) {
          o.setData({
            userList: t.data
          });
        }
      });
    },
    putSign: function (num){
      var o = this;
      a.util.request({
        url: "entry/wxapp/sign",
        cachetime: "0",
        data: {
          num: num,
        },
        header: {
          "content-type": "application/json"
        },
        dataType: "json",
        success: function (t) {
          o.setData({
            num: t.num
          });
        }
      });
    },
  getData: function (session_key,appid ) {
    var o = this;

    wx.getWeRunData({
      success: function (res) {
        console.log(res);
        console.log("appid:" + appid + "session_key:" + session_key + "encryptedData:" + res.encryptedData + "iv:" + res.iv);
        var encryptedData = res.encryptedData;
        var iv = res.iv;
        //使用解密工具，链接地址：
        //https://codeload.github.com/gwjjeff/cryptojs/zip/master
        var pc = new WXBizDataCrypt(appid, session_key);
        console.log(pc);
        var data = pc.decryptData(encryptedData, iv)
        if (data){
          var step = parseInt(data.stepInfoList[data.stepInfoList.length - 1].step);
          console.log(step);
            if(step>0){
              o.setData({step:step});
              o.putSign(step);
            }
        }
        console.log();
      },
      fail: function (res) {
        wx.showModal({
          title: '提示',
          content: '开发者未开通微信运动，请关注“微信运动”公众号后重试',
          showCancel: false,
          confirmText: '知道了'
        })
      }
    })
  },
  onShow: function () {
  
      
      //this.getRecommand()
    },

    authorize: function() {
        this.setData({
            is__authorize: true,
        })
    },
    // 选择时间
    ShowPricker: function (e) {
        this.setData({
            isShowPricker: true,
        })
    },

  // 选择时间
  ShowPrickers: function () {

    this.setData({
      isShowPrickers: true,
    })
  },
    bindChange: function (e) {
        this.setData({
          price_id: e.detail.value,
        });
        let val = e.detail.value;
    },
    ClosePricker: function () {
        this.setData({
            isShowPricker: false,
        })
    },
    ConfirmPricker: function (e) {
        this.setData({
            isShowPricker: false,
        });
        wx.navigateTo({
          url: '../pay/index?isTiao=0&price_id='+this.data.price_id[0]
        })
    },

  bindChanges: function (e) {
    if (e.detail.value){
      this.setData({
        price_id: e.detail.value,
      });
      let val = e.detail.value;
    }else{
      wx.showToast({
        title: '请选择金额',
        icon: 'none',
        duration: 500
      });
    }
  },
  ClosePrickers: function () {
    this.setData({
      isShowPrickers: false,
    })
  },
  ConfirmPrickers: function (e) {
    console.log(this.data.price_id[0]);
    this.setData({
      isShowPrickers: false,
    });
    wx.navigateTo({
      url: '../pay/index?isTiao=1&price_id=' + this.data.price_id[0]
    })
  },
	onShareAppMessage: function (ops) {
		return {
      title: '微信运动挑战',
			path: 'pages/index/index?op=' + getApp().getOpenId,
			success: function (res) {
				console.log("转发成功:" + JSON.stringify(res));
			},
			fail: function (res) {
				console.log("转发失败:" + JSON.stringify(res));
			}
		}
	}
})